// class.js
const classApp = createApp({
    data() {
        return {
            classes: [],
            majors: [],
            colleges: [],
            loading: false,
            error: null,
            formData: {
                classCode: '',
                className: '',
                majorCode: '',
                collegeCode: '',
                enrollmentYear: ''
            }
        }
    },
    methods: {
        // 加载班级数据
        async loadClasses() {
            this.loading = true;
            this.error = null;
            try {
                const response = await fetch(window.contextPath+'/class/listAll');
                const majorsResponse = await fetch(window.contextPath+'/major/listAll');
                const collegesResponse = await fetch(window.contextPath+'/college/listAll');
                if (response.ok && majorsResponse.ok && collegesResponse.ok) {
                    this.classes = await response.json();
                    this.majors = await majorsResponse.json();
                    this.colleges = await collegesResponse.json();
                } else {
                    const errorText = await response.text();
                    this.error = '加载数据失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            }catch ( error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }finally {
                this.loading = false;
            }
        },
        // 添加班级信息
        async addClass() {
            try {
                console.log(this.formData);
                const response = await fetch(window.contextPath+'/class/add', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(this.formData)
                });

                if (response.ok) {
                    alert('添加成功')
                    await this.loadClasses();
                    this.formData = {
                        classCode: '',
                        className: '',
                        majorCode: '',
                        collegeCode: '',
                        enrollmentYear: ''
                    };
                    closeClassAddModal();
                } else {
                    const errorText = await response.text();
                    this.error = '添加班级信息失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }
        },
        // 删除班级信息
        async deleteClass(classCode,majorCode,collegeCode) {
            console.log(classCode,majorCode,collegeCode);
            try {
                if (!confirm('确定要删除该班级信息吗？')) {
                    return;
                }

                const response = await fetch(window.contextPath+'/class/delete', {
                    method: 'DELETE',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        classCode: classCode,
                        majorCode: majorCode,
                        collegeCode: collegeCode
                    })
                });

                if (response.ok) {
                    alert('删除成功')
                    await this.loadClasses();
                } else {
                    const errorText = await response.text();
                    this.error = '删除班级失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }
        },
        // 修改班级信息
        async updateClass() {
            alert('该功能正在开发中...')
        },

        // 当选择专业时，自动更新学院代码
        updateCollegeCode() {
            if (this.formData.majorCode) {
                // 查找选中的专业对象
                const selectedMajor = this.majors.find(major => major.majorCode === this.formData.majorCode);
                if (selectedMajor) {
                    // 更新 formData 中的 collegeCode
                    this.formData.collegeCode = selectedMajor.collegeCode;
                }
            } else {
                // 如果未选择专业，则清空学院代码
                this.formData.collegeCode = '';
            }
        }
    },
    mounted() {
        window.classAppInstance = this;
    }
});
classApp.mount('#class-content');

// 显示添加班级表单
function showClassAddModal() {
    const modal = document.getElementById('classAddModal');
    modal.classList.add('show');
    modal.style.display = 'flex';
}

// 关闭添加班级表单
function closeClassAddModal() {
    document.getElementById('classAddModal').style.display = 'none';
}
